package com.frontend.api.controller;

import com.common.exception.ModelNotFoundException;
import com.common.model.Tags;
import com.common.service.TagsService;
import com.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "Tags", value = "标签模块")
public class TagsController {

    @Resource
    TagsService tagsService;

    @ApiOperation("获取热门标签")
    @RequestMapping(value = "/tags/hot", method = RequestMethod.GET)
    public CommonResult<List<Tags>> getHotTags(
            @RequestParam(value = "size", required = false, defaultValue = "5") int size
    ) {
        return CommonResult.success(tagsService.selectHostTags(size));
    }

    @ApiOperation("获取标签列表")
    @RequestMapping(value = "/tags", method = RequestMethod.GET)
    public CommonResult<List<Tags>> getTags() {
        List<Tags> tags = tagsService.selectAllTags();
        return CommonResult.success(tags);
    }

    @ApiOperation("根据id标签信息")
    @RequestMapping(value = "/tags/{id}", method = RequestMethod.GET)
    public CommonResult<Tags> getSingleTags(
            @PathVariable("id") Long tagsId
    ) {
        try {
            Tags tags = tagsService.selectOneTagById(tagsId);
            return CommonResult.success(tags);
        } catch (ModelNotFoundException e) {
            return CommonResult.failed("ID参数错误");
        }
    }

}
